{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests # pip install requests 安装做网络请求的库\n",
    "from bs4 import BeautifulSoup # pip install bs4 安装bs4\n",
    "\n",
    "headers = {\n",
    "    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'\n",
    "}\n",
    "# 向服务器发送GET请求\n",
    "r = requests.get('https://www.tadu.com/', headers=headers)\n",
    "# print(r.status_code, r.connection, r.url, r.request)\n",
    "# print(type(r.text), len(r.text))\n",
    "# print(r.content) # 获取到的网站html原码\n",
    "# print(r.headers)\n",
    "# print(dir(r))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "{'titile': '仙妻如云', 'intro': '       “苏惊蛰，你也老大不小了，嫂子且来问你。你要道侣不要？“\\r\\n   “嫂子，谁啊？”\\r\\n   “我……”\\r\\n    女修都是刮骨钢刀，本打算在仙侠世界平淡修仙的苏惊蛰，'}\n"
     ]
    }
   ],
   "source": [
    "soup = BeautifulSoup(r.content, 'html.parser')\n",
    "# print(soup.prettify()) # 加入缩进显示Html代码,便于人类的阅读\n",
    "bookBox = soup.find('div', {'class': 'bookBox'})\n",
    "booklist = bookBox.find('div', class_='booklist')\n",
    "items = booklist.find_all('a')\n",
    "item = items[0]\n",
    "title = item.find('div', class_='bookinfo').find('h2').text\n",
    "intro = item.find('div', class_='desc').text\n",
    "articles = []\n",
    "for item in items:\n",
    "    d = {}\n",
    "    d['titile'] = item.find('div', class_='bookinfo').find('h2').text\n",
    "    d['intro'] = item.find('div', class_='desc').text\n",
    "    articles.append(d)\n",
    "print(len(items))\n",
    "print(articles[0])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
